<?php
class IndexAction extends BaseAction{
	function index(){
		$sign = array();
		$list = M("Config")->select();
		foreach($list as $key=>$val){
			$sign[$val['key']] = $val['val'];
		}
		$this->assign('sign',$sign);
		$this->display('index');
	}

	function login(){
		if(IS_POST){
			$sign = D("User")->where("name='{$_POST['name']}'")->find();
			if($sign){
				if(md5($_POST['pwd']) && $sign['pwd']){
					session('login_user',$sign);
					cookie('login_user',$sign);
					$this->redirect("Index/index");
				}else{
					$this->error('密码错误,请重试...');
				}
			}else{
				$this->error('用户不存在,请重试...');
			}
		}else{
			$this->display('login');
		}
	}

	function loginOut(){
		session('login_user',null);
		cookie('login_user',null);
		$this->redirect('Index/login');
	}

	function setting(){
		if(IS_AJAX){
			$rsts = array();
			foreach($_POST as $key=>$val){
				$sign = D("Config")->where("`key`='$key'")->getField('key');
				$rst = $sign ? D("Config")->where("`key`='$key'")->save(array('val'=>$val)) : D("Config")->add(array('key'=>$key,'val'=>$val));
				$rsts[] = $rst!==false ? 1 : 0;
			}
			if(!in_array(0,$rsts))
				$this->ajaxReturn('success','设置成功',1);
			else
				$this->ajaxReturn('error','设置失败',0);
		}else{
			$list = M("Config")->select();
			$sign = array();
			foreach($list as $key=>$val){
				$sign[$val['key']] = $val['val'];
			}
			$this->assign('sign',$sign);
			$this->display('setting');
		}
	}
}
?>